package algorithm;

import java.util.LinkedList;

public class T00071 {
    public static void main(String[] args) {

    }

    static class Solution {
        public String simplifyPath(String path) {
            String[] arr = path.split("/");
            LinkedList<String> list = new LinkedList<>();
            for (int i = 0; i < arr.length; i ++) {
                String s = arr[i];
                if (s == null || ".".equals(s) || "".equals(s)) {
                    continue;
                } else if ("..".equals(s)) {
                    if (!list.isEmpty()) list.removeLast();
                } else {
                    list.addLast(s);
                }
            }


            StringBuilder builder = new StringBuilder();
            if (!list.isEmpty()) {
                while (!list.isEmpty()) {
                    builder.append("/");
                    builder.append(list.removeFirst());
                }
            } else {
                builder.append("/");
            }

            return builder.toString();
        }
    }
}
